﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mrc.Config
{
    public class ConfigCenterBuilder : IConfigCenterBuilder
    {

        private readonly List<Action<IServiceCollection>> _servicesDelegates;

        private readonly List<Action<IConfigurationBuilder>> _configureBuildDelegates;

        public IConfigCenterBuilder AddConfiguration(Action<IConfigurationBuilder> builder)
        {
            if (builder!=null)
            {
                _configureBuildDelegates.Add(builder);
            }
            return this;
        }

        public IConfigCenterBuilder AddService(Action<IServiceCollection> configServiceAction)
        {
            if (configServiceAction != null)
            {
                _servicesDelegates.Add(configServiceAction);
            }
            return this;
        }

        public IServiceCollection Build(IServiceCollection services)
        {
            CommonServiceBuild(services);
            var configBuilder = new ConfigurationBuilder();
            foreach (var configureBuildAction in _configureBuildDelegates)
            {
                configureBuildAction(configBuilder);
            }
            configBuilder.Build();
            return services;
        }

        private IServiceCollection CommonServiceBuild(IServiceCollection services)
        {
            foreach (var serviceAction in _servicesDelegates)
            {
                serviceAction(services);
            }
            return services;
        }

        public IConfigCenterBuilder InitConfiguration()
        {
            throw new NotImplementedException();
        }
    }
}
